<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\common\model\Category;

/**
 * 接收消息类型接口
 */
class Goods extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Goods();
    }

    public function add()
    {
        if ($this->request->isPost())
        {
            $this->dataLimit = true;
            $params = $this->request->post();
            if ($params)
            {
                if ($this->dataLimit && $this->dataLimitFieldAutoFill)
                {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    $result = $this->model->allowField(true)->save($params);
                    if ($result !== false)
                    {
                        $this->success('发布成功！请等待审核');
                    }
                    else
                    {
                        $this->error($this->model->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        } else {
            $categoryList = Category::getCategoryArray('default');

            $data = [];

            if ($categoryList) {
                foreach ($categoryList as $k => $v) {
                    $data[$k]['id'] = $v['id'];
                    $data[$k]['name'] = $v['name'];
                }
            }


            $this->success('', $data);
        }
    }

    public function index(){

        $this->limitFields = 'id,category_id,title,description,province,city,area,updatetime,changes_num,total_num,status';
        list($where, $sort, $order, $offset, $limit , $field) = $this->buildparams();

        $total = $this->model
            ->where($where)
            ->where(function($query) {
                $query->where('status',1);
            })
            ->order($sort, $order)
            ->count();

        $list = $this->model
            ->field($field)
            ->where($where)
            ->where(function($query) {
                $query->where('status',1);
            })
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();

//        $this->success('sql:',$this->model->getLastSql());
        $result = array("total" => $total, "rows" => $list);

        $this->success('',$result);
    }

    /**
     * 详情
     */
    public function detail($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));

        $orderRecord = \app\common\model\Order::getOrderByGoodsId($ids);
        $isBuy = \app\common\model\Order::isUserBuy($this->auth->id,$ids);

        $goodsModel = new \app\common\model\Goods();
        $newList = $goodsModel->where('status',1)->order('id desc')->limit(5)->select();
        $this->success('',[
            'detail' => $row,
            'orderRecord' => $orderRecord,
            'isBuy' => $isBuy,
            'newList' => $newList
        ]);
    }




}